package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.developer.apps.actions.AppSubmitAction;
import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.pojos.TBodyRecord;
import com.wy.xinzhi.common.pojos.TCustomer;
import com.wy.xinzhi.common.pojos.TFoodRecord;
import com.wy.xinzhi.common.services.BodyRecordService;
import com.wy.xinzhi.common.services.CustomerService;
import com.wy.xinzhi.common.services.FoodRecordService;

@ParentPackage("json")
@Namespace(value="/xz")
@SuppressWarnings({"rawtypes","unused"})
public class BodyRecordAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(BodyRecordAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	private List<Map<String,Object>> bodyRecList;
	
	private List date_data ;
	
	private List kal_data ;
	
	private String jsonRes;
	
	private TBodyRecord bodyRecord;
	
	private TCustomer customer;
	//页面参数
	private int page;
	private int pageSize;
	private int total;
	//排序参数
	private String orderCol;
	private String order;
	
	@Autowired
	private BodyRecordService bodyRecordService;
	@Autowired
	private CustomerService customerService;
	
	public List<Map<String, Object>> getBodyRecList() {
		return bodyRecList;
	}


	public void setBodyRecList(List<Map<String, Object>> bodyRecList) {
		this.bodyRecList = bodyRecList;
	}


	@Action(value = "saveBodyRec", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String saveBodyRec() throws UnsupportedEncodingException, ServiceException, ParseException{
		String reId =ServletActionContext.getRequest().getParameter("reId");
		String openId =ServletActionContext.getRequest().getParameter("openId");
		String userId=ServletActionContext.getRequest().getParameter("userId");
		String height =ServletActionContext.getRequest().getParameter("height");
		String weight =ServletActionContext.getRequest().getParameter("weight");
		String thigh =ServletActionContext.getRequest().getParameter("thigh");
		String waist =ServletActionContext.getRequest().getParameter("waist");
		String stomach =ServletActionContext.getRequest().getParameter("stomach");
		String bellies =ServletActionContext.getRequest().getParameter("bellies");
		String createDate =ServletActionContext.getRequest().getParameter("createDate");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		TBodyRecord record = null;
		//修改
		if(reId!=null&&!"".equals(reId))
		{
			 record = this.bodyRecordService.getBodyRecordById(reId);
		}
		//新增
		else{
			record =new TBodyRecord();
			Date date = createDate.equals("")?new Date():sdf.parse(createDate);
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
			record.setReId(sdf1.format(date)+"-"+openId);
			record.setCustomerId(openId);
			record.setUserId(userId);
		}
		
		record.setHeight(height==null?0:Integer.parseInt(height));
		record.setWeight(weight==null?0:Integer.parseInt(weight));
		record.setThigh(thigh==null?0:Integer.parseInt(thigh));
		record.setWaist(waist==null?0:Integer.parseInt(waist));
		record.setStomach(stomach==null?0:Integer.parseInt(stomach));
		record.setBellies(bellies==null?0:Integer.parseInt(bellies));
		record.setCreateDate(createDate.equals("")?new Timestamp(new Date().getTime()):new Timestamp(sdf.parse(createDate).getTime()));
		
		this.bodyRecordService.saveBodyRec(record);
		return SUCCESS;
	}
	
	@Action(value = "getBodyRecById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String getBodyRecById() throws UnsupportedEncodingException{
			String reId =ServletActionContext.getRequest().getParameter("reId");
			this.bodyRecord= this.bodyRecordService.getBodyRecordById(reId);
		return SUCCESS;
	}
	
	@Action(value = "deleteBodyRecById", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String deleteBodyRecById() throws UnsupportedEncodingException{
			String reId =ServletActionContext.getRequest().getParameter("reId");
			this.bodyRecordService.deleteBodyRecById(reId);
		return SUCCESS;
	}
	
	@Action(value = "queryBodyRecord", 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
					"false"}) })
	public String queryBodyRecord() throws UnsupportedEncodingException{
		String customerId =ServletActionContext.getRequest().getParameter("openId");
		List<Map<String, Object>> resultList= this.bodyRecordService.queryBodyRecord(customerId);
		this.bodyRecList = resultList;
		customer=this.customerService.loginValid(customerId);
		return SUCCESS;
	}
	
	
	public String getJsonRes() {
		return jsonRes;
	}
	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}

	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List getDate_data() {
		return date_data;
	}

	public void setDate_data(List date_data) {
		this.date_data = date_data;
	}

	public List getKal_data() {
		return kal_data;
	}

	public void setKal_data(List kal_data) {
		this.kal_data = kal_data;
	}

	public TCustomer getCustomer() {
		return customer;
	}

	public void setCustomer(TCustomer customer) {
		this.customer = customer;
	}


	public TBodyRecord getBodyRecord() {
		return bodyRecord;
	}


	public void setBodyRecord(TBodyRecord bodyRecord) {
		this.bodyRecord = bodyRecord;
	}
	
}
